﻿using NovoSerDes.Comm;
using NovoSerDes.Funcs;
using NovoSerDesEva;
using NovoUI.UserCtrl;
using OfficeOpenXml;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;

namespace NovoSerDes.Forms
{
    public partial class Log : UIPage
    {
        private ILogData _logData;
        private LogControl _logControls;
        private ConfigState _configState;

        private Thread monitorThread;
        private bool isMonitoring;
        public Log()
        {
            _logData = LogData.Instance;
            InitializeComponent();
            _logControls = new LogControl();
            _logControls.ChangeRowDefaultCellStyle();
            this.uiPanel2.Controls.Add(_logControls);
            _logControls.Dock = DockStyle.Fill;
            uiRadioButton1_CheckedChanged(radio_AutoSave,null);
        }

        public LogControl LogControl
        {
            get { return _logControls; }
        }
        


        private void csvToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            // 显示一个消息框，询问用户是否拆分 Excel 文件
            DialogResult result = MessageBox.Show("是否根据Chip Address拆分 CSV 文件？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            // 检查用户的选择
            if (result == DialogResult.Yes)
            {
                // 用户选择了是
                // 在这里执行相关操作
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string filePath = saveFileDialog.FileName;

                    // 调用 SaveDataSetToCsv 方法，将 DataSet 保存为 CSV 文件
                    _logData.SaveDataSetToCsv(_logData.GetDataSet, filePath);
                }
            }
            else
            {
                // 用户选择了否，或者点击了关闭按钮
                // 在这里执行其他操作
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string filePath = saveFileDialog.FileName;

                    // 调用 SaveDataSetToCsv 方法，将 DataSet 保存为 CSV 文件
                    _logData.SaveDataSetToCsv(_logData.GetDataTableAllData, filePath);
                }
            }
            
        }

        private void excelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 显示一个消息框，询问用户是否拆分 Excel 文件
            DialogResult result = MessageBox.Show("是否根据Chip Address拆分 Excel 文件？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            // 检查用户的选择
            if (result == DialogResult.Yes)
            {
                // 用户选择了是
                // 在这里执行相关操作
                // 打开文件对话框，让用户选择保存文件的路径
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string filePath = saveFileDialog.FileName;

                    // 调用 SaveDataSetToExcel 方法，将 DataSet 保存为 Excel 文件
                    _logData.SaveDataSetToExcel(_logData.GetDataSet, filePath);
                }
            }
            else
            {
                // 用户选择了否，或者点击了关闭按钮
                // 在这里执行其他操作
                // 打开文件对话框，让用户选择保存文件的路径
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取用户选择的文件路径
                    string filePath = saveFileDialog.FileName;

                    // 调用 SaveDataSetToExcel 方法，将 DataSet 保存为 Excel 文件
                    _logData.SaveDataSetToExcel(_logData.GetDataTableAllData, filePath);
                }
            }
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LogControl.Highlights("","",txtAddr.Text);
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            LogControl.FilterData(txtChipAddr.Text, txtType.Text);
        }

        private void uiRadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            UIRadioButton radioButton = sender as UIRadioButton;

            if (radioButton != null)
            {
                if (radioButton.Checked)
                {
                    StartMonitoring();
                }
                else
                {
                    StopMonitoring();
                }
            }
        }

        private void StartMonitoring()
        {
            //if (monitorThread == null || !monitorThread.IsAlive)
            //{
            //    isMonitoring = true;
            //    monitorThread = new Thread(MonitoringTask);
            //    monitorThread.Start();
            //}
            LogData.StartMonitoring();
        }

        private void StopMonitoring()
        {
            //isMonitoring = false;
            //if (monitorThread != null && monitorThread.IsAlive)
            //{
            //    monitorThread.Join();
            //}
            LogData.StopMonitoring();
        }

        private void MonitoringTask()
        {
            while (isMonitoring)
            {
                LogData.StartMonitoring();
                Thread.Sleep(10000); // 每隔10秒执行一次
            }
        }

        private void LogControl_ValueChanged(object sender, bool value)
        {
            NovoCommWIthLog.LogSwtich = value;
        }

        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            NovoCommWIthLog.LogSwtich = value;
        }

        private void txtChipAddr_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                btnFilter_Click(sender,e);
            }
            //if(e.KeyCode == Keys.Back)
            //{
            //    if(txtChipAddr.Text == "" || txtType.Text == "")
            //    {
            //        btnFilter_Click(sender, e);
            //    }
            //}
        }

        private void btnSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(sender,e);
            }
        }
    }
}
